﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using HalalService.Models;

namespace HalalService.Controllers
{
    public class HalalItemsController : ApiController
    {
        private HalalServiceContext db = new HalalServiceContext();

        // GET: api/HalalItems
        public IQueryable<HalalItem> GetHalalItems()
        {
            return db.HalalItems;
        }

        // GET: api/HalalItems/5
        [ResponseType(typeof(HalalItem))]
        public IHttpActionResult GetHalalItem(string id)
        {
            HalalItem halalItem = db.HalalItems.Find(id);
            if (halalItem == null)
            {
                return NotFound();
            }

            return Ok(halalItem);
        }

        // PUT: api/HalalItems/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutHalalItem(string id, HalalItem halalItem)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != halalItem.Id)
            {
                return BadRequest();
            }

            db.Entry(halalItem).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HalalItemExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/HalalItems
        [ResponseType(typeof(HalalItem))]
        public IHttpActionResult PostHalalItem(HalalItem halalItem)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.HalalItems.Add(halalItem);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (HalalItemExists(halalItem.Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = halalItem.Id }, halalItem);
        }

        // DELETE: api/HalalItems/5
        [ResponseType(typeof(HalalItem))]
        public IHttpActionResult DeleteHalalItem(string id)
        {
            HalalItem halalItem = db.HalalItems.Find(id);
            if (halalItem == null)
            {
                return NotFound();
            }

            db.HalalItems.Remove(halalItem);
            db.SaveChanges();

            return Ok(halalItem);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool HalalItemExists(string id)
        {
            return db.HalalItems.Count(e => e.Id == id) > 0;
        }
    }
}